package com.dayuanit.dy15.spider.runnable;

import com.dayuanit.dy15.spider.entity.Commodity;
import com.dayuanit.dy15.spider.entity.Navigation;
import com.dayuanit.dy15.spider.mapper.CommodityMapper;
import com.dayuanit.dy15.spider.mapper.NavigationMapper;
import com.dayuanit.dy15.spider.pool.SpiderThreadPool;
import com.dayuanit.dy15.spider.util.ApiConnector;
import com.dayuanit.dy15.spider.util.SleepUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.Date;
import java.util.UUID;

public class BookInfoRunnable implements Runnable {
    private String url;
    private String price;
    private String thirdNavName;

    private CommodityMapper commodityMapper;
    private NavigationMapper navigationMapper;

    public BookInfoRunnable(String url, String price, String thirdNavName, CommodityMapper commodityMapper, NavigationMapper navigationMapper) {
        this.url = url;
        this.commodityMapper = commodityMapper;
        this.price = price;
        this.thirdNavName = thirdNavName;
        this.navigationMapper = navigationMapper;
    }

    @Override
    public void run() {

        SleepUtil.sleep();

        //访问书本详情
        String html = ApiConnector.get(url);
        Document document = Jsoup.parse(html);

        String bookName = document.select("h1.bookname_box").select("span.title_words").first().text();
        String author = document.select("div.explain_box").select("p#author").select("span").first().text();
        String profile = document.select("div.newEdit_box").first().text();
        String pictureUrl = document.select("div.bookCover_area").select("img").first().attr("src");
        String pictureName = UUID.randomUUID().toString().replace("-", "") + ".jpg";

        System.out.println("书名=" + bookName + " 作者=" + author);

        //提交下载图片任务给线程池
        SpiderThreadPool.downLoadThreadPool.execute(new DownLoadRunnable(pictureUrl, pictureName));

        Navigation thirdNav = navigationMapper.getByName(thirdNavName, 3);
        Navigation secondNav = navigationMapper.selectByPrimaryKey(thirdNav.getParentNavId());

        //商品 入库
        Commodity commodity = new Commodity();
        commodity.setAuthor(author);
        commodity.setCommoName(bookName);
        commodity.setCreateTime(new Date());

        commodity.setPrice(price);
        commodity.setProfile(profile);
        commodity.setPublisher("");
        commodity.setPublisherTime("");

        commodity.setFirstNavId(secondNav.getParentNavId());
        commodity.setSecondNavId(secondNav.getId());
        commodity.setThirdNavId(thirdNav.getId());

        commodity.setStock(10);
        commodity.setWordNum("10万");
        commodity.setPictureName(pictureName);
        commodityMapper.insert(commodity);

        //TODO 下载图片

    }

    public static void main(String[] args) {
        String html = ApiConnector.get("http://e.dangdang.com/products/1901068492.html");
        Document document = Jsoup.parse(html);
        String bookName = document.select("h1.bookname_box").select("span.title_words").first().text();
        System.out.println(bookName);
        String author = document.select("div.explain_box").select("p#author").select("span").first().text();
        System.out.println(author);

        String text = document.select("div.newEdit_box").first().text();
        System.out.println(text);
    }
}
